/**
 * DesktopDownload.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.view.download;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import webcrawler.activities.data.Data;
import webcrawler.activities.view.utils.ExtendedComboBox;
import webcrawler.activities.view.utils.OptionItem;
import webcrawler.activities.webcrawler.WebCrawler;
import activities.base.xml.XmlConstants;

/**
 * The Class DesktopDownload.
 * 
 * @author Gerson Groth
 */
public class DesktopDownload extends JDialog {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The content panel. */
	private final JPanel contentPanel = new JPanel();

	/** The jtf keyword. */
	private JTextField jtfKeyword;

	/** The jtf starting near. */
	private JTextField jtfStartingNear;

	/** The jtf xml. */
	private JTextField jtfXml;

	/** The jtf tcx. */
	private JTextField jtfTcx;

	/** The jtf gpx. */
	private JTextField jtfGpx;

	/** The jtf display name. */
	private JTextField jtfDisplayName;

	/** The jtf distance init. */
	private JTextField jtfDistanceInit;

	/** The jtf distance end. */
	private JTextField jtfDistanceEnd;

	/** The jtf time init. */
	private JTextField jtfTimeInit;

	/** The jtf time end. */
	private JTextField jtfTimeEnd;

	/** The jtf elevation init. */
	private JTextField jtfElevationInit;

	/** The jtf elevation end. */
	private JTextField jtfElevationEnd;

	/** The jdc date end. */
	private com.toedter.calendar.JDateChooser jdcDateEnd;

	/** The jdc date init. */
	private com.toedter.calendar.JDateChooser jdcDateInit;

	/** The jcb activity type. */
	private javax.swing.JComboBox<OptionItem> jcbActivityType;

	/** The jcb event type. */
	private javax.swing.JComboBox<OptionItem> jcbEventType;

	/** The jcb time period. */
	private javax.swing.JComboBox<OptionItem> jcbTimePeriod;

	/** The chckbx xml file. */
	private JCheckBox chckbxXmlFile;

	/** The chckbx tcx file. */
	private JCheckBox chckbxTcxFile;

	/** The chckbx gpx file. */
	private JCheckBox chckbxGpxFile;

	/** The chckbx my activities only. */
	private JCheckBox chckbxMyActivitiesOnly;

	/** The tcx file chooser. */
	private JFileChooser tcxFile;

	/** The xml file chooser. */
	private JFileChooser xmlFile;

	/** The gpx file chooser. */
	private JFileChooser gpxFile;

	/** The tcx path. */
	private String tcxPath = "";

	/** The xml path. */
	private String xmlPath = "";

	/** The gpx path. */
	private String gpxPath = "";

	/**
	 * Create the dialog.
	 */
	public DesktopDownload() {
		setFileChoosers();

		setFrameConfigurations();

		createExplorePanel();

		createFiltersPanel();

		createDestinationFolderPanel();

		createSearchButton();

		createComponents();
	}

	/**
	 * Sets the file choosers.
	 */
	private void setFileChoosers() {
		tcxFile = new JFileChooser();
		xmlFile = new JFileChooser();
		gpxFile = new JFileChooser();
		tcxFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		xmlFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		gpxFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	}

	/**
	 * Creates the search button.
	 */
	private void createSearchButton() {
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				search();
			}
		});
		btnSearch.setBounds(170, 561, 89, 23);
		contentPanel.add(btnSearch);

	}

	/**
	 * Creates the destination folder panel.
	 */
	private void createDestinationFolderPanel() {
		JPanel destinationFolderPanel = new JPanel();
		destinationFolderPanel.setLayout(null);
		destinationFolderPanel.setBorder(new TitledBorder(UIManager

		.getBorder("TitledBorder.border"), "Destination Folder",

		TitledBorder.LEADING, TitledBorder.TOP, null, null));
		destinationFolderPanel.setBounds(10, 410, 414, 140);
		contentPanel.add(destinationFolderPanel);

		JPanel panelXml = createPanelXml();
		destinationFolderPanel.add(panelXml);
		destinationFolderPanel.add(chckbxXmlFile);

		JPanel panelTcx = createPanelTcx();
		destinationFolderPanel.add(panelTcx);
		destinationFolderPanel.add(chckbxTcxFile);

		JPanel panelGpx = createPanelGpx();
		destinationFolderPanel.add(panelGpx);
		destinationFolderPanel.add(chckbxGpxFile);

	}

	/**
	 * Creates the panel gpx.
	 * 
	 * @return the j panel
	 */
	private JPanel createPanelGpx() {
		JPanel gpxPanel = new JPanel();
		gpxPanel.setLayout(null);
		gpxPanel.setBounds(89, 25, 315, 23);

		final JButton btnSearchGpx = new JButton("Search");
		btnSearchGpx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				searchButtonGpx(arg0);
			}
		});
		btnSearchGpx.setBounds(226, 0, 89, 23);
		gpxPanel.add(btnSearchGpx);

		jtfGpx = new JTextField();
		jtfGpx.setColumns(10);
		jtfGpx.setBounds(0, 1, 216, 20);
		gpxPanel.add(jtfGpx);

		chckbxGpxFile = new JCheckBox("Gpx File");
		chckbxGpxFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if (chckbxGpxFile.isSelected()) {
					jtfGpx.setEnabled(true);
					btnSearchGpx.setEnabled(true);
				} else {
					jtfGpx.setText("");
					jtfGpx.setEnabled(false);
					btnSearchGpx.setEnabled(false);
				}
			}
		});
		chckbxGpxFile.setSelected(true);
		chckbxGpxFile.setBounds(6, 25, 77, 23);

		return gpxPanel;
	}

	/**
	 * Creates the panel tcx.
	 * 
	 * @return the j panel
	 */
	private JPanel createPanelTcx() {
		JPanel tcxPanel = new JPanel();
		tcxPanel.setLayout(null);
		tcxPanel.setBounds(89, 61, 315, 23);

		final JButton btnSearchTcx = new JButton("Search");
		btnSearchTcx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchButtonTcx(e);
			}
		});
		btnSearchTcx.setBounds(226, 0, 89, 23);
		tcxPanel.add(btnSearchTcx);

		jtfTcx = new JTextField();
		jtfTcx.setColumns(10);
		jtfTcx.setBounds(0, 1, 216, 20);
		tcxPanel.add(jtfTcx);

		chckbxTcxFile = new JCheckBox("Tcx File");
		chckbxTcxFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (chckbxTcxFile.isSelected()) {
					jtfTcx.setEnabled(true);
					btnSearchTcx.setEnabled(true);
				} else {
					jtfTcx.setText("");
					jtfTcx.setEnabled(false);
					btnSearchTcx.setEnabled(false);
				}
			}
		});
		chckbxTcxFile.setSelected(true);
		chckbxTcxFile.setBounds(6, 61, 77, 23);

		return tcxPanel;
	}

	/**
	 * Creates the panel xml.
	 * 
	 * @return the j panel
	 */
	private JPanel createPanelXml() {
		JPanel xmlPanel = new JPanel();
		xmlPanel.setLayout(null);
		xmlPanel.setBounds(89, 97, 315, 23);

		final JButton btnSearchXml = new JButton("Search");
		btnSearchXml.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchButtonXml(e);
			}
		});
		btnSearchXml.setBounds(226, 0, 89, 23);
		xmlPanel.add(btnSearchXml);

		jtfXml = new JTextField();
		jtfXml.setColumns(10);
		jtfXml.setBounds(0, 1, 216, 20);
		xmlPanel.add(jtfXml);

		chckbxXmlFile = new JCheckBox("Xml File");
		chckbxXmlFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (chckbxXmlFile.isSelected()) {
					jtfXml.setEnabled(true);
					btnSearchXml.setEnabled(true);
				} else {
					jtfXml.setText("");
					jtfXml.setEnabled(false);
					btnSearchXml.setEnabled(false);
				}
			}
		});
		chckbxXmlFile.setSelected(true);
		chckbxXmlFile.setBounds(6, 97, 77, 23);

		return xmlPanel;
	}

	/**
	 * Creates the filters panel.
	 */
	private void createFiltersPanel() {
		JPanel filtersPanel = new JPanel();
		filtersPanel.setBounds(10, 108, 410, 291);
		filtersPanel.setBorder(new TitledBorder(null, "Filters",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPanel.add(filtersPanel);
		filtersPanel.setLayout(null);

		createActivityType(filtersPanel);

		createEventType(filtersPanel);

		createTimePeriod(filtersPanel);

		createDistance(filtersPanel);

		createTime(filtersPanel);

		createElevation(filtersPanel);

		createDisplayName(filtersPanel);

		createMyActivitiesOnly(filtersPanel);

		createCustomDates(filtersPanel);

	}

	/**
	 * Creates the custom dates.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	private void createCustomDates(JPanel filtersPanel) {

		JPanel panelCustomDates = new JPanel();
		panelCustomDates.setBounds(10, 195, 224, 85);
		filtersPanel.add(panelCustomDates);
		panelCustomDates.setLayout(null);

		JLabel lblCustomDates = new JLabel("Custom Dates:");
		lblCustomDates.setBounds(0, 11, 83, 14);
		panelCustomDates.add(lblCustomDates);

		JLabel label = new JLabel("-");
		label.setBounds(106, 39, 4, 14);
		panelCustomDates.add(label);

		jdcDateInit = new com.toedter.calendar.JDateChooser();
		jdcDateInit.setDateFormatString("MM/dd/yyyy");
		jdcDateInit.setBounds(0, 36, 100, 20);
		jdcDateInit.setEnabled(false);
		panelCustomDates.add(jdcDateInit);

		jdcDateEnd = new com.toedter.calendar.JDateChooser();
		jdcDateEnd.setDateFormatString("MM/dd/yyyy");
		jdcDateEnd.setBounds(120, 36, 100, 20);
		jdcDateEnd.setEnabled(false);
		panelCustomDates.add(jdcDateEnd);

	}

	/**
	 * Creates the my activities only.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	private void createMyActivitiesOnly(JPanel filtersPanel) {
		chckbxMyActivitiesOnly = new JCheckBox("My activities only");
		chckbxMyActivitiesOnly.setBounds(270, 79, 128, 23);
		filtersPanel.add(chckbxMyActivitiesOnly);
	}

	/**
	 * Creates the display name.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	private void createDisplayName(JPanel filtersPanel) {
		JLabel lblDisplayName = new JLabel("Display Name:");
		lblDisplayName.setBounds(270, 27, 99, 14);
		filtersPanel.add(lblDisplayName);

		jtfDisplayName = new JTextField();
		jtfDisplayName.setBounds(270, 52, 130, 20);
		filtersPanel.add(jtfDisplayName);
		jtfDisplayName.setColumns(10);

	}

	/**
	 * Creates the elevation.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	private void createElevation(JPanel filtersPanel) {
		JLabel lblElevationGain = new JLabel("Elevation Gain:");
		lblElevationGain.setBounds(160, 139, 85, 14);
		filtersPanel.add(lblElevationGain);

		jtfElevationInit = new JTextField();
		jtfElevationInit.setColumns(10);
		jtfElevationInit.setBounds(160, 164, 31, 20);
		filtersPanel.add(jtfElevationInit);

		JLabel label_2 = new JLabel("-");
		label_2.setBounds(195, 167, 4, 14);
		filtersPanel.add(label_2);

		jtfElevationEnd = new JTextField();
		jtfElevationEnd.setColumns(10);
		jtfElevationEnd.setBounds(203, 164, 31, 20);
		filtersPanel.add(jtfElevationEnd);

	}

	/**
	 * Creates the time.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	private void createTime(JPanel filtersPanel) {
		JLabel lblTime = new JLabel("Time:");
		lblTime.setBounds(160, 83, 46, 14);
		filtersPanel.add(lblTime);

		jtfTimeInit = new JTextField();
		jtfTimeInit.setColumns(10);
		jtfTimeInit.setBounds(160, 108, 31, 20);
		filtersPanel.add(jtfTimeInit);

		JLabel label_1 = new JLabel("-");
		label_1.setBounds(195, 111, 4, 14);
		filtersPanel.add(label_1);

		jtfTimeEnd = new JTextField();
		jtfTimeEnd.setColumns(10);
		jtfTimeEnd.setBounds(203, 108, 31, 20);
		filtersPanel.add(jtfTimeEnd);

	}

	/**
	 * Creates the distance.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	private void createDistance(JPanel filtersPanel) {
		JLabel lblDistance = new JLabel("Distance:");
		lblDistance.setBounds(160, 27, 46, 14);
		filtersPanel.add(lblDistance);

		jtfDistanceInit = new JTextField();
		jtfDistanceInit.setBounds(160, 52, 31, 20);
		filtersPanel.add(jtfDistanceInit);
		jtfDistanceInit.setColumns(10);

		jtfDistanceEnd = new JTextField();
		jtfDistanceEnd.setColumns(10);
		jtfDistanceEnd.setBounds(203, 52, 31, 20);
		filtersPanel.add(jtfDistanceEnd);

		JLabel label = new JLabel("-");
		label.setBounds(195, 55, 4, 14);
		filtersPanel.add(label);

	}

	/**
	 * Creates the time period.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	@SuppressWarnings("unchecked")
	private void createTimePeriod(JPanel filtersPanel) {
		JLabel lblTimePeriod = new JLabel("Time Period:");
		lblTimePeriod.setBounds(10, 139, 74, 14);
		filtersPanel.add(lblTimePeriod);

		jcbTimePeriod = new ExtendedComboBox();
		jcbTimePeriod.setBounds(10, 164, 128, 20);
		filtersPanel.add(jcbTimePeriod);

		jcbTimePeriod.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				OptionItem time = (OptionItem) jcbTimePeriod.getSelectedItem();
				if ("customTimestamp".equals(time.getValue())) {
					jdcDateEnd.setEnabled(true);
					jdcDateInit.setEnabled(true);
				} else {
					jdcDateEnd.setEnabled(false);
					jdcDateInit.setEnabled(false);
				}
			}
		});

		// chckbxMyActivitiesOnly.addChangeListener(new ChangeListener() {
		// public void stateChanged(ChangeEvent e) {
		// }
		// });

	}

	/**
	 * Creates the event type.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	@SuppressWarnings("unchecked")
	private void createEventType(JPanel filtersPanel) {
		JLabel lblEventType = new JLabel("Event Type:");
		lblEventType.setBounds(10, 83, 74, 14);
		filtersPanel.add(lblEventType);

		jcbEventType = new ExtendedComboBox();
		jcbEventType.setBounds(10, 108, 128, 20);
		filtersPanel.add(jcbEventType);

	}

	/**
	 * Creates the activity type.
	 * 
	 * @param filtersPanel
	 *            the filters panel
	 */
	@SuppressWarnings("unchecked")
	private void createActivityType(JPanel filtersPanel) {
		JLabel lblActivityType = new JLabel("Activity Type:");
		lblActivityType.setBounds(10, 27, 74, 14);
		filtersPanel.add(lblActivityType);

		jcbActivityType = new ExtendedComboBox();
		jcbActivityType.setBounds(10, 52, 128, 20);

		filtersPanel.add(jcbActivityType);
	}

	/**
	 * Creates the explore panel.
	 */
	private void createExplorePanel() {
		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 410, 86);
		panel.setBorder(new TitledBorder(null, "Explore", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		contentPanel.add(panel);
		panel.setLayout(null);

		JLabel lblKeyword = new JLabel("Keyword:");
		lblKeyword.setBounds(10, 24, 93, 14);
		panel.add(lblKeyword);

		jtfKeyword = new JTextField();
		jtfKeyword.setBounds(10, 49, 186, 20);
		panel.add(jtfKeyword);
		jtfKeyword.setColumns(10);

		JLabel lblStartingNear = new JLabel("Starting near:");
		lblStartingNear.setBounds(214, 24, 93, 14);
		panel.add(lblStartingNear);

		jtfStartingNear = new JTextField();
		jtfStartingNear.setBounds(214, 49, 186, 20);
		panel.add(jtfStartingNear);
		jtfStartingNear.setColumns(10);

	}

	/**
	 * Sets the frame configurations.
	 */
	private void setFrameConfigurations() {
		setTitle("Desktop Activities Search");
		setBounds(100, 100, 446, 633);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

	}

	/**
	 * Search button gpx.
	 * 
	 * @param e
	 *            the e
	 */
	protected void searchButtonGpx(ActionEvent e) {
		int result = gpxFile.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			gpxPath = gpxFile.getSelectedFile().getAbsolutePath();
			while (gpxPath.indexOf('\\') != -1) {
				gpxPath = gpxPath.replace('\\', '/');
			}
			this.jtfGpx.setText(gpxPath);
		}

	}

	/**
	 * Search button tcx.
	 * 
	 * @param e
	 *            the e
	 */
	protected void searchButtonTcx(ActionEvent e) {
		int result = tcxFile.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			tcxPath = tcxFile.getSelectedFile().getAbsolutePath();
			while (tcxPath.indexOf('\\') != -1) {
				tcxPath = tcxPath.replace('\\', '/');
			}
			this.jtfTcx.setText(tcxPath);
		}

	}

	/**
	 * Search button xml.
	 * 
	 * @param e
	 *            the e
	 */
	private void searchButtonXml(ActionEvent e) {
		int result = xmlFile.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			xmlPath = xmlFile.getSelectedFile().getAbsolutePath();
			while (xmlPath.indexOf('\\') != -1) {
				xmlPath = xmlPath.replace('\\', '/');
			}
			this.jtfXml.setText(xmlPath);
		}

	}

	/**
	 * Replace all.
	 * 
	 * @param s
	 *            the s
	 * @return the string
	 */
	private String replaceAll(String s) {
		while (s.indexOf('\\') != -1) {
			s = s.replace('\\', '/');
		}
		return s;
	}

	/**
	 * Creates the activity type combo box.
	 */
	@SuppressWarnings("unchecked")
	private void createActivityTypeComboBox() {
		ExtendedComboBox ex = (ExtendedComboBox) jcbActivityType;
		ex.addItem(new OptionItem("Any Activity Type", "all"));
		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Running", "running"));

		ex.addItem(new OptionItem("     Street Running", "street_running"));
		ex.addItem(new OptionItem("     Track Running", "track_running"));
		ex.addItem(new OptionItem("     Trail Running", "trail_running"));
		ex.addItem(new OptionItem("     Treadmill Running", "treadmill_running"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Cycling", "cycling"));

		ex.addItem(new OptionItem("     Cyclocross", "cyclocross"));
		ex.addItem(new OptionItem("     Downhill Biking", "downhill_biking"));
		ex.addItem(new OptionItem("     Indoor Cycling", "indoor_cycling"));
		ex.addItem(new OptionItem("     Mountain Biking", "mountain_biking"));
		ex.addItem(new OptionItem("     Recumbent Cycling", "recumbent_cycling"));
		ex.addItem(new OptionItem("     Road Cycling", "road_biking"));
		ex.addItem(new OptionItem("     Track Cycling", "track_cycling"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Fitness Equipment", "fitness_equipment"));

		ex.addItem(new OptionItem("     Elliptical", "elliptical"));
		ex.addItem(new OptionItem("     Indoor Cardio", "indoor_cardio"));
		ex.addItem(new OptionItem("     Indoor Rowing", "indoor_rowing"));
		ex.addItem(new OptionItem("     Stair Climbing", "stair_climbing"));
		ex.addItem(new OptionItem("     Strength Training", "strength_training"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Hiking", "hiking"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Swimming", "swimming"));

		ex.addItem(new OptionItem("     Lap Swimming", "lap_swimming"));
		ex.addItem(new OptionItem("     Open Water Swimming",
				"open_water_swimming"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Walking", "walking"));

		ex.addItem(new OptionItem("     Casual Walking", "casual_walking"));
		ex.addItem(new OptionItem("     Speed Walking", "speed_walking"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Transition", "transition"));

		ex.addItem(new OptionItem("     Swim to Bike Transition",
				"swimToBikeTransition"));
		ex.addItem(new OptionItem("     Bike to Run Transition",
				"bikeToRunTransition"));
		ex.addItem(new OptionItem("     Run to Bike Transition",
				"runToBikeTransition"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Motorcycling", "motorcycling"));

		ex.addDelimiter("----------------------------------");
		ex.addItem(new OptionItem("Other", "other"));

		ex.addItem(new OptionItem("     Backcountry Skiing/Snowboarding",
				"backcountry_skiing_snowboarding"));
		ex.addItem(new OptionItem("     Boating", "boating"));
		ex.addItem(new OptionItem("     Cross Country Skiing",
				"cross_country_skiing"));
		ex.addItem(new OptionItem("     Driving", "driving_general"));
		ex.addItem(new OptionItem("     Flying", "flying"));
		ex.addItem(new OptionItem("     Golf", "golf"));
		ex.addItem(new OptionItem("     Horseback Riding", "horseback_riding"));
		ex.addItem(new OptionItem("     Inline Skating", "inline_skating"));
		ex.addItem(new OptionItem("     Mountaineering", "mountaineering"));
		ex.addItem(new OptionItem("     Paddling", "paddling"));
		ex.addItem(new OptionItem("     Resort Skiing/Snowboarding",
				"resort_skiing_snowboarding"));
		ex.addItem(new OptionItem("     Rowing", "rowing"));
		ex.addItem(new OptionItem("     Sailing", "sailing"));
		ex.addItem(new OptionItem("     Skate Skiing", "skate_skiing"));
		ex.addItem(new OptionItem("     Skating", "skating"));
		ex.addItem(new OptionItem("     Snowmobiling", "snowmobiling"));
		ex.addItem(new OptionItem("     Snowshoeing", "snow_shoe"));
		ex.addItem(new OptionItem("     Stand Up Paddleboarding",
				"stand_up_paddleboarding"));
		ex.addItem(new OptionItem("     Whitewater Kayaking/Rafting",
				"whitewater_rafting_kayaking"));
		ex.addItem(new OptionItem("     Wind/Kite Surfing", "wind_kite_surfing"));

	}

	/**
	 * Creates the event type combo box.
	 */
	private void createEventTypeComboBox() {
		jcbEventType.addItem(new OptionItem("Any Event Type", "all"));
		jcbEventType.addItem(new OptionItem("Geocaching", "geocaching"));
		jcbEventType.addItem(new OptionItem("Fitness", "fitness"));
		jcbEventType.addItem(new OptionItem("Recreation", "recreation"));
		jcbEventType.addItem(new OptionItem("Race", "race"));
		jcbEventType.addItem(new OptionItem("Special Event", "specialEvent"));
		jcbEventType.addItem(new OptionItem("Training", "training"));
		jcbEventType
				.addItem(new OptionItem("Transportation", "transportation"));
		jcbEventType.addItem(new OptionItem("Touring", "touring"));
		jcbEventType.addItem(new OptionItem("Uncategorized", "uncategorized"));
	}

	/**
	 * Creates the time period combo box.
	 */
	private void createTimePeriodComboBox() {
		jcbTimePeriod
				.addItem(new OptionItem("Last 30 Days", "previousDays=30"));
		jcbTimePeriod
				.addItem(new OptionItem("Last 24 Hours", "previousDays=1"));
		jcbTimePeriod.addItem(new OptionItem("Last 7 Days", "previousDays=7"));
		jcbTimePeriod.addItem(new OptionItem("Last 365 Days",
				"previousDays=365"));
		jcbTimePeriod
				.addItem(new OptionItem("Custom Dates", "customTimestamp"));
	}

	/**
	 * Creates the combo boxes.
	 */
	private void createComboBoxes() {
		createActivityTypeComboBox();
		createEventTypeComboBox();
		createTimePeriodComboBox();
	}

	/**
	 * Creates the components.
	 */
	private void createComponents() {
		createComboBoxes();

	}

	/**
	 * Search.
	 */
	private void search() {
		this.setVisible(false);
		Data data = Data.getInstance().getClone();

		if (!"".equals(xmlPath.trim()) && !xmlPath.endsWith("/"))
			xmlPath += "/";
		data.setXmlFolder(xmlPath);
		if (!"".equals(tcxPath.trim()) && !tcxPath.endsWith("/"))
			tcxPath += "/";
		data.setTcxFolder(tcxPath);
		if (!"".equals(gpxPath.trim()) && !gpxPath.endsWith("/"))
			gpxPath += "/";
		data.setGpxFolder(gpxPath);

		data.setKeyword(jtfKeyword.getText());
		data.setLocation(jtfStartingNear.getText());

		OptionItem item = (OptionItem) jcbActivityType.getSelectedItem();
		data.setActivityType(item.getValue());

		item = (OptionItem) jcbEventType.getSelectedItem();
		data.setEventType(item.getValue());

		item = (OptionItem) jcbTimePeriod.getSelectedItem();
		data.setTimePeriod(item.getValue());

		if (item.getValue().equals(XmlConstants.CUSTOM_DATE)) {
			Date date = jdcDateInit.getDate();

			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
			if (date != null) {
				data.setDateInit(df.format(date));
			} else {
				data.setDateInit("");
			}

			date = jdcDateEnd.getDate();
			if (date != null) {
				data.setDateEnd(df.format(date));
			} else {
				data.setDateEnd("");
			}

		}

		data.setDistanceInit(jtfDistanceInit.getText());
		data.setDistanceEnd(jtfDistanceEnd.getText());

		data.setTimeInit(jtfTimeInit.getText());
		data.setTimeEnd(jtfTimeEnd.getText());

		data.setElevationInit(jtfElevationInit.getText());
		data.setElevationEnd(jtfElevationEnd.getText());

		data.setDisplayName(jtfDisplayName.getText());

		data.setMyActivitiesOnly(chckbxMyActivitiesOnly.isSelected());

		if (chckbxGpxFile.isSelected() || chckbxTcxFile.isSelected()
				|| chckbxXmlFile.isSelected()) {
			data.setXmlFolder(replaceAll(jtfXml.getText()));
			data.setTcxFolder(replaceAll(jtfTcx.getText()));
			data.setGpxFolder(replaceAll(jtfGpx.getText()));
			data.setDownloadXml(chckbxXmlFile.isSelected());
			data.setDownloadGpx(chckbxGpxFile.isSelected());
			data.setDownloadTcx(chckbxTcxFile.isSelected());

			WebCrawler crawler = new WebCrawler(data);
			Thread webCrawler = new Thread(crawler);
			webCrawler.start();

			dispose();

		} else {
			JOptionPane.showMessageDialog(null,
					"Choose at least one file type to download");
		}

	}
}
